#include "cpdfcanvas.h"
#include <poppler/cpp/poppler-document.h>
#include "cpdfreader.h"

CPdfCanvas::CPdfCanvas()
{
    p_page = NULL;
    m_xres = 72.0;
    m_yres = 72.0;
    m_scale = 1.0;
    m_ori = poppler::rotate_0;
    p_reader = NULL;
}

CPdfCanvas::~CPdfCanvas()
{
    //dtor
    if( p_page != NULL )delete p_page;
    p_page = NULL;
}

CPdfCanvas::CPdfCanvas( CPdfReader* reader , poppler::page *page,
               poppler::rotation_enum orient  ,
               double scale  , double xres , double yres )
{
    p_img = NULL;
    if( reader != NULL ){

        p_page = page;

        if( p_page == NULL ){
            throw ERR_CAN_NOT_CREATE_PAGE;
        }
        m_xres = xres;
        m_yres = yres;
        m_scale = scale;
        m_ori = orient;
        p_reader = reader;

        m_pos.x = 0;
        m_pos.y = 0;
    }
}

void CPdfCanvas::Scale( double s )
{
    if( p_page == NULL ){
        m_err = ERR_PAGE_NOT_EXIST;
#if defined( __DEBUG__)
        std::cerr << "FILE:" << __FILE__ << "At(" << __LINE__ <<") specified poppler page not exist." << std::endl;
#else
        std::cerr << "specified poppler page not exist." << std::endl;
#endif // defined

        return;
    }
    if( do_render( p_page ) == 0 )
        m_scale = s;
}

int CPdfCanvas::Render( poppler::page *page )
{
    return do_render( page );
}

int CPdfCanvas::Render()
{
    return do_render( p_page  );
}

void CPdfCanvas::SetOrient( poppler::rotation_enum orient )
{
     m_ori = orient;
     do_render( p_page );
}

int CPdfCanvas::do_render( poppler::page *page )
{
    int ret = 0;

    if( page != NULL ){
        wxRect                  rect;
        poppler::image          img;
        poppler::page_renderer  render;
        render.set_render_hints( 0x01 | 0x02 | 0x04 );

        {//生成图片
            img = render.render_page(page , m_xres * m_scale , m_yres * m_scale, -1, -1 , -1 , -1 , m_ori );
            char *data = img.data();
            char *buff = NULL;

            if( data != NULL ){
                size_t size = img.height() * img.width() * 4;
                buff = (char*)malloc( img.height() * img.width() * 3 );

                m_size.x = img.width();
                m_size.y = img.height();

                if( buff == NULL ) return -1;

                for( register size_t i = 0 , j = 0; i < size ; i += 4 ){
                    buff[ j ] = data[ i + 2 ];
                    buff[ j + 1 ] = data[ i + 1 ];
                    buff[ j + 2 ] = data[ i ];
                    j += 3;
                }
            }
            if( p_img != NULL ) delete p_img;
            p_img = new wxImage( img.width() , img.height()  );
            p_img->SetData( (unsigned char*)buff );
        }

    }
    else{//页面无效
        m_err = ERR_PAGE_NOT_EXIST;
#if defined( __DEBUG__)
        std::cerr << "FILE:" << __FILE__ << "At(" << __LINE__ <<") specified poppler page not exist." << std::endl;
#else
        std::cerr << "specified poppler page not exist." << std::endl;
#endif // defined

        ret = (int)m_err;
    }
    return ret;
}






